import time
import re
import scrapy
from m3u8 import M3U8
from mjtt.items import MjttItem

'''
https://www.mjtta.cc/p/443835/264/1388575
https://vip.lz-cdn4.com/20220521/13343_21556af8/1000k/hls/mixed.m3u8
https://vip.lz-cdn4.com/20220521/13343_21556af8/1000k/hls/26015e88e86000000.ts
'''
class MjtvSpider(scrapy.Spider):
    name = "mjtv"
    # allowed_domains = ["mjtv.com"]
    start_urls = ["https://www.mjtta.cc/p/443835/264/1388575"]

    # 解析html
    def parse(self, response):
        print('开始解析html')
        # print(response.text)
        url_header = response.xpath('//div[@id="tem_vod_box"]/script/text()').get()
        # print(url_header)
        url_header = re.findall("src='(.*?)'", url_header)[0]
        # print(url_header)
        # https://jx.wujinkk.com/dplayer/?url=https://vip.lz-cdn4.com/20220521/13343_21556af8/index.m3u8
        print('解析请求视频参数成功')
        url_header = url_header.split("index")[0].split("url=")[1]
        # print(url_header)
        # 开始构建请url
        # https://vip.lz-cdn4.com/20220521/13343_21556af8/
        # https://vip.lz-cdn4.com/20220521/13343_21556af8/index.m3u8
        url_m3u8 = url_header + 'index.m3u8'
        # print(url_m3u8)
        yield scrapy.Request(url=url_m3u8,callback=self.parse_1,meta={'url_header':url_header})

    # 解析m3u8文件，创建ts_url
    def parse_1(self, response):
        url_header = response.meta['url_header']
        # m3u8解析函数
        m3u8 = response.text
        m3u8 = M3U8(m3u8)
        if m3u8.is_variant:
            jishuqi = 1
            # #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000,RESOLUTION=1080x608
            # 1000k/hls/mixed.m3u8
            for i in m3u8.playlists:
                print('文件是变体流')
                m3u8_url = url_header + i.uri
                print(m3u8_url)
                print('提交成功')
                # 递归调用自身方法
                url_header_1 = m3u8_url.split('mixed')[0]
                yield scrapy.Request(url=m3u8_url,callback=self.parse_1,meta={'url_header':url_header,'url_header_1':url_header_1})
        else:
            print('文件是媒体playlist')
            url_header_1 = response.meta['url_header_1']
            for i in m3u8.segments:
                # ts视频url
                # https://vip.lz-cdn4.com/20220521/13343_21556af8/
                # https://vip.lz-cdn4.com/20220521/13343_21556af8/1000k/hls/26015e88e86000000.ts
                ts_url = url_header_1 + i.uri
                # print(ts_url)
                yield scrapy.Request(ts_url,callback=self.parse_2,meta={'title':i.uri})
                # break

    # 请求ts视频并保存
    def parse_2(self, response):
        title = response.meta['title']
        ts_con = response.body
        # print(title)
        # 创建数据对象
        mjttitem = MjttItem()
        mjttitem['title'] = title
        mjttitem['ts_con'] = ts_con
        yield mjttitem
